// pages/dishes_set/dishes_set.js
const tool = require('../../tool/Holidays.js');
const getdata = require("../../tool/GetDate.js")
const util = require("../../utils/util.js")
var app = getApp();
var currentPage = 0;
var listData = [];
var districtID = 0;
var hallID = 0;
var dishesID = 0;
var attributeID = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    diningHallArray: [],
    objectDiningHallArray: [],
    index: 0,
    dishesList: [
      // {
      //   name:"青菜",
      //   dishesClassName:"素菜",
      //   attributeName:"周一",
      //   imgUrl: "meal.png",
      // },
      // {
      //   name:"煎鸡蛋",
      //   dishesClassName:"荤菜",
      //   imgUrl: "meal.png",
      // },
      // {
      //   name:"海蛎煎",
      //   dishesClassName:"荤菜",
      //   attributeName:"周二",
      //   imgUrl: "meal.png",
      // },
    ],
    attributeArray: [],
    objectAttributeArray: [],
    attributeIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    currentPage = 0;
    listData = [];
    districtID = 0;
    hallID = 0;
    dishesID = 0;
    attributeID = 0;
  },

  getDiningHall: function (districtID) {
    let that = this;
    //根据获取到的区域信息加载食堂
    getdata.getdata({
      url: "gbt/BasicInfo/DiningHall/",
      data: districtID,
      success: function (res) {
        console.log(res.root);
        if (res.root) {
          let diningHallIndex = 0;
          if (hallID == 0) {
            //暂时默认
            hallID = res.root.diningHallArray[diningHallIndex].id;
            that.setData({
              index: diningHallIndex,
            });
          }
          listData = [];
          currentPage = 1;
          that.getAttributeInfo(districtID, hallID);

          that.setData({
            diningHallArray: res.root.diningHallTitleArray,
            objectDiningHallArray: res.root.diningHallArray,
          });
        }
      },
    });
  },

  getAttributeInfo: function (districtID, hallID) {
    let that = this;
    //菜品属性 需提供食堂ID 
    getdata.getdata({
      url: "gbt/BasicInfo/AttributePicker/" + hallID + "/",
      data: districtID,
      success: function (res) {
        console.log(res.root);
        if (res.root) {
          let attributeList = res.root.arributeTitleArray;
          attributeList.unshift("全部");
          let attributeIndex = 0;
          if (attributeID == 0) {
            //默认属性ID
            attributeID = "";
            that.setData({
              attributeIndex: attributeIndex,
            });
          }

          listData = [];
          currentPage = 1;
          that.getDishes(districtID, hallID, attributeID);

          that.setData({
            attributeArray: res.root.arributeTitleArray,
            objectAttributeArray: res.root.arributeArray,
          });
        }
      }
    });
  },

  getDishes: function (districtID, hallID, attributeID) {
    let that = this;
    let param = {};
    param["currentPage"] = currentPage;
    param["districtID"] = districtID;
    param["hallID"] = hallID;
    param["attributeID"] = attributeID;
    getdata.postdata({
      url: "gbt/BasicInfo/DishesSet",
      data: JSON.stringify(param),
      success: function (res) {
        console.log(res.root);
        if (res.root) {
          let list = res.root.data;
          for (let dataIndex = 0; dataIndex < list.length; dataIndex++) {
            listData.push(list[dataIndex]);
          }
          that.setData({
            dishesList: listData,
          });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    districtID = app.globalData.myUserData.districtID;
    this.getDiningHall(districtID);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    currentPage++;
    // this.getDiningHall(districtID);
    this.getDishes(districtID, hallID, attributeID);
  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {

  // },

  bindDiningHallPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let hall = this.data.objectDiningHallArray[e.detail.value];
    hallID = hall.id;
    currentPage = 1;
    listData = [];
    this.getDishes(districtID, hallID, attributeID);

    this.setData({
      index: e.detail.value
    });
  },

  bindAttributePickerChange: function (e) {
    let attribute = {};
    if (e.detail.value == 0) {
      //默认属性ID
      attributeID = "";
    } else {
      attribute = this.data.objectAttributeArray[e.detail.value-1];
      attributeID = attribute.id;
    }
    currentPage = 1;
    listData = [];
    this.getDishes(districtID, hallID, attributeID);

    this.setData({
      attributeIndex: e.detail.value
    });
  },

  onAdd: function (e) {
    // console.log(e);
    wx.navigateTo({
      url: '/pages/dishes_detail/dishes_detail?state=add&hallID=' + hallID,
    })
  },

  onDetail: function (e) {
    console.log(e);
    wx.navigateTo({
      url: '/pages/dishes_detail/dishes_detail?state=modify&hallID=' + hallID + '&id=' + e.detail.id,
    })
  },
})